################################################################################
#### SET-UP ####################################################################
################################################################################

### Clear workspace
rm(list=ls())

### Define home directory
#setwd(home.dir <- "~/Dropbox (Personal)/2-Shared/Work/Collaborations/Dafoe/Honor/Replication-Final")
setwd(home.dir <- "/Users/Allan/Dropbox/1Papers/1Reputation/Honor-Culture-War/2012-05/Replication-Final")

### Load libraries
stopifnot(require(foreign))
stopifnot(require(Matching)) 
stopifnot(require(plyr)) 
stopifnot(require(dplyr))
stopifnot(require(matlab)) 
stopifnot(require(ggplot2))
stopifnot(require(ordinal)) 
stopifnot(require(reshape2)) 
stopifnot(require(car))
stopifnot(require(TeachingDemos)) 
stopifnot(require(directlabels))
stopifnot(require(NPC))
stopifnot(require(grid))

ToDate <- function (numeric.date) {
  as.Date(numeric.date, origin = "1970-01-01")
}
mysw <- function (...) {
  setwd(paste(..., sep = "/"))
}
FileName <- function (path=NULL, name=NULL, ext=NULL, replace=FALSE) {
  p <- paste(path, collapse = "")
  n <- paste(name, collapse = "")
  e <- paste(".", ext, sep = "")
  d <- format(Sys.Date(), "%y%m%d") 
  for (l in seq_along(letters)) {
    if (l > 1) old_file_name <- file_name
    file_name <- paste0(p, d, paste(n, letters[l], sep="-"), e)
    if (!any(grepl(file_name, list.files()))) {
      if (replace) file_name <- ifelse(l > 1, old_file_name, file_name)
      break
    }
  }
  cat("\nFile name:", file_name, "\n\n")
  return(file_name)
}
SourceLatest <- function (name, dir=".", ...) {
  all.files <- list.files(dir)
  files <- all.files[grepl(name, all.files) & grepl("\\.R\\>", all.files)]
  dates <- sub(".*([0-9]{6}).*", "\\1", files)
  file <- paste(dir, files[which.max(dates)], sep="/")
  cat("\nOpening:", files[which.max(dates)], "\n")
  source(file, ...)
}

mysw(home.dir, "5-Figures/out")
txt.out <- FileName("Figures",, "out")
txtStart(file=txt.out)
getwd()

### Load data (four South codings)
mysw(home.dir, "3-Analysis-Data")
## 1: G. W. Bush Southern, Truman non-Southern (original coding)
mid <- read.dta("15-03-13-USMIDs1.dta")

## better labels
mid$LeaderName <- Recode(mid$Leader, "
'AdamsJ' = 'J. Adams';
'AdamsJQ' = 'J. Q. Adams';
'BushGHW' = 'G. H. W. Bush';
'BushGW' = 'G. W. Bush';
'HarrisonB' = 'Harrison';
'HarrisonWH' = 'W. Harrison';
'JohnsonA' = 'A. Johnson';
'JohnsonLB' = 'L. Johnson';
'RooseveltFD' = 'F. Roosevelt';
'RooseveltT' = 'T. Roosevelt';
'Van_Buren' = 'Van Buren'
")

### Define subsets of data
mid <- mutate(mid,
              EndedWithinTerm = Pres2 == "",
              ## Non-fishing MIDs
              nonfishing = FishDisp == 0 & !is.na(FishDisp),
              ## Non-fishing MIDs where both countries were originators
              primary = Primary == 1 & FishDisp == 0 & !is.na(Primary),
              ## Non-fishing MIDs where both countries were alone on their side
              bilateral = BothAlone == 1 & FishDisp == 0 & !is.na(BothAlone),
              ## Presidents covered by time range of MID dataset (1816-2010)
              in.data = TermEndDate1 > as.Date("1816-01-01") &
                  TermStartDate1 < as.Date("2010-06-01"),
              ## Presidents who experienced at least one non-fishing MID
              non0nofish = nMIDsNoF > 0 & !is.na(nMIDsNoF),
              ## Presidents who experienced at least one primary MID
              non0prim = nMIDsPrim > 0 & !is.na(nMIDsPrim),
              ## Presidents that experienced at least one bilateral MID
              non0bilat = nMIDsAlone > 0 & !is.na(nMIDsAlone),
              ## First presidential observation in each MID subset
              lf = LeaderOb == 1 & non0nofish,
              lp = LeaderOb == 1 & non0prim,
              lb = LeaderOb == 1 & non0bilat,
              SouthFactor = factor(ifelse(South1 == 0, 'Non-South',
                  'South')))

################################################################################
##### CREATE VARIABLES #########################################################
################################################################################

mysw(home.dir, "4-Analysis")
SourceLatest("CreateVars", echo = TRUE, max.deparse.length = Inf)

################################################################################
##### PLOT RAW DATA ############################################################
################################################################################

# raw.npc <- NPC(data = subset(mid, bilateral), test.statistic = 'DiffMeans',
#                alternative = "greater",
#                tr.var = "SouthFactor", tr.label = "South",
#                clust.var = "Leader", event.var = "EndedWithinTerm",
#                y.vars = c('ForceUS', 'DurationUS', 'OutcomeUS'),
#                comb.fun = "NormalCF", FWE.adj = TRUE, na.rm = TRUE,
#                n.perms = 100000, seed = 1, print.steps = TRUE,
#                return.matrix = TRUE)

(pres.bilateral.df <- group_by(subset(mid, bilateral), LeaderName) %>%
     summarise(Region = unique(as.character(SouthFactor)),
               MIDs = n(),
               Force = mean(ForceUS),
               Duration = mean(DurationUS),
               Outcome = mean(OutcomeUS))
 )

north.color <- 'blue'
south.color <- 'grey30'
northx <- -1.5
southx <- 1.5
colvec <- c(north.color, south.color)
pres.bilateral.melt <- melt(pres.bilateral.df,
                        id.vars = c("LeaderName", "Region", "MIDs"))
pres.bilateral.melt <- mutate(pres.bilateral.melt,
                              xpos = ifelse(Region == 'South', southx, northx),
                              hjust = ifelse(Region == 'South', 0, 1),
                              ypos = value)
pres.bilateral.melt <- ddply(pres.bilateral.melt, ~Region + variable, mutate,
                         Mean = mean(value),
                             WtdMean = weighted.mean(value, MIDs))
pres.bilateral.melt <- ddply(pres.bilateral.melt, ~Region + variable + value
                             , mutate, ##+ MIDs
                             Uob = seq_along(value),
                             Nsame = n(),
                             label = paste(as.character(LeaderName),
                                 collapse = ' & '))
pres.bilateral.melt <- arrange(pres.bilateral.melt, variable, Region, -value)
outcome.df <- droplevels(subset(pres.bilateral.melt, variable == 'Outcome'))

#### Force ####

force.df <- droplevels(subset(pres.bilateral.melt, variable == 'Force'))
force.df <- mutate(force.df
                  , label = ifelse(LeaderName %in% c('Fillmore', 'Harrison'),
                          'Fillmore & Harrison', label)
                  , ypos = ifelse(label == 'Fillmore & Harrison', ypos + .02, ypos)
                 , label = ifelse(LeaderName %in% c('Kennedy', 'Arthur'),
                         'Arthur & Kennedy', label)
                 , ypos = ifelse(label == 'Arthur & Kennedy', ypos - .02, ypos)
                 , label = ifelse(LeaderName %in% c('Truman', 'Van Buren'),
                                  'Truman & Van Buren', label)
                 , label = ifelse(LeaderName %in% c('Harding', 'Hayes'),
                                  'Harding & Hayes', label)
                 , ypos = ifelse(label == 'Harding & Hayes', ypos - .04, ypos)
                 , label = ifelse(LeaderName %in% c('Lincoln', 'Taft'),
                        'Lincoln & Taft', label)
                 , ypos = ifelse(label == 'Lincoln & Taft', ypos - .005, ypos)
                 , label = ifelse(LeaderName %in% c('Grant', 'Obama'),
                                  'Grant & Obama', label)
                 , ypos = ifelse(label == 'Grant & Obama', ypos + .015, ypos)
                 ## , label = ifelse(LeaderName %in% c('Taft'), '& Taft', label)
                 , ypos = ifelse(label == 'Reagan', ypos + .015, ypos)
                 , ypos = ifelse(label == 'Pierce', ypos - .005, ypos)
                 , label = ifelse(LeaderName %in% c('Madison', 'Monroe'),
                                  'Madison & Monroe', label)
                 , ypos = ifelse(label == 'Madison & Monroe', ypos, ypos)
                 , label = ifelse(LeaderName %in% c('Tyler'),
                                  'Tyler', label)
                 , ypos = ifelse(label == 'Clinton & Jackson', ypos+0.015, ypos)
                 , ypos = ifelse(label == 'Tyler', ypos-0.02, ypos)
                 , ypos = ifelse(label == 'A. Johnson', ypos + .01, ypos)
                 , ypos = ifelse(label == 'Polk', ypos - .01, ypos)
                 , ypos = ifelse(label == 'Monroe', ypos + .02, ypos)
                 , ypos = ifelse(label == 'Clinton', ypos + .03, ypos)
                 , ypos = ifelse(label == 'Jackson', ypos + .01, ypos)
                 ## , hjust = ifelse(label == 'Grant',  1 - hjust, hjust)
                   )

force.df$label[duplicated(force.df$label)] <- ""
xoff <- 0.43
mysw(home.dir, "5-Figures")

dfmeans <- data.frame(x=c(northx, southx), 
                      y=c(mean(force.df$WtdMean[force.df$Region == 'Non-South']),
                          mean(force.df$WtdMean[force.df$Region == 'South'])),
                      colvec=colvec)


pdf(file = FileName('PresForceBilateral', '', 'pdf', replace = TRUE),
    width = 4, height = 8)
(ggplot(data = subset(force.df),
                      #, #Uob == 1 | LeaderName %in% c('Kennedy')),
        aes(color = Region))
 + facet_wrap(~variable)
 + geom_point(data=dfmeans, aes(x,y),  colour=colvec,  shape=18, size=6)
 + annotate('segment', linetype = 'solid', size = 1.3,
            xend = northx+2, x = northx - 2,
            y = mean(force.df$WtdMean[force.df$Region == 'Non-South']),
            yend = mean(force.df$WtdMean[force.df$Region == 'Non-South']),
            color = north.color)
 + annotate('text', label = 'mean', x = northx - 2.2, hjust = 1,
            color = north.color,
            y = mean(force.df$WtdMean[force.df$Region == 'Non-South']))
 + annotate('segment', linetype = 'solid', size = 1.3, xend = southx-2,
            color = south.color,
            x = southx + 2,
            y = mean(force.df$WtdMean[force.df$Region == 'South']),
            yend = mean(force.df$WtdMean[force.df$Region == 'South']))
 + annotate('text', label = 'mean', x = southx + 2.2, hjust = 0,
            color = south.color,
            y = mean(force.df$WtdMean[force.df$Region == 'South']))
  + geom_point(aes(x = xpos, y = value, ## alpha = 1/Nsame, 
                  size = MIDs),
              shape = 1)
 + geom_text(aes(x = xpos + ifelse(hjust == 1, -xoff, xoff),
                 y = ypos, label = label, ## alpha = 1/Nsame,
                 hjust = hjust), size = 3)
 + theme_bw()
 + labs(y = "Proportion of Disputes with U.S. Use of Force", x = NULL)
 + scale_alpha_identity()
 + scale_color_manual(values = c(north.color, south.color))
 + scale_y_continuous(breaks = c(min(force.df$value), .2, .4, .6, .8,
                          max(force.df$value)))
 + scale_x_continuous(breaks = c(northx, southx)
                    , labels = c('Non-Southern                ', '         Southern')   ##NULL
                    , limits = c(-8, 8)
                      )
 + theme(axis.text.x = element_text(colour=colvec), axis.text=element_text(size=14))
 + theme(panel.grid.minor = element_blank(), axis.ticks = element_blank(),
         strip.text = element_text(size = 20))
 + guides(size = FALSE, color = FALSE)
)
dev.off()


#### Duration ####

duration.df <- droplevels(subset(pres.bilateral.melt, variable == 'Duration'))

minvalue <- round(min(duration.df$value),0)
maxvalue <- round(max(duration.df$value),0)
diffvalue <- maxvalue-minvalue

duration.df <- mutate(duration.df
                      , label = ifelse(LeaderName %in% c('Arthur', 'Hayes'),
                                       'Arthur & Hayes', label)
                      , ypos = ifelse(label=='Arthur & Hayes', ypos-6, ypos)
                      , label = ifelse(LeaderName %in% c('Harding'),
                                       'Harding', label)
                      , ypos = ifelse(LeaderName %in% c('Harding'), ypos-1, ypos)
                      , ypos = ifelse(LeaderName %in% c('Ford'), ypos-1, ypos)
                      , ypos = ifelse(LeaderName %in% c('Eisenhower', 'Ford'), ypos+2, ypos)
                      , ypos = ifelse(LeaderName %in% c('Reagan'), ypos+5, ypos)
                      , ypos = ifelse(LeaderName %in% c('Pierce'), ypos+4, ypos)
                      , ypos = ifelse(LeaderName %in% c('Nixon'), ypos+4, ypos)
                      , label = ifelse(LeaderName %in% c('Van Buren', 'Lincoln'),
                                       'Van Buren & Lincoln', label)
                      , ypos = ifelse(LeaderName %in% c('Van Buren', 'Lincoln'), 
                                      duration.df$ypos[duration.df$LeaderName=="Lincoln"]-1, ypos)
                      , label = ifelse(LeaderName %in% c('Cleveland', 'Obama'),
                                       'Cleveland & Obama', label)
                      , ypos = ifelse(LeaderName %in% c('Cleveland', 'Obama'),
                                      duration.df$ypos[duration.df$LeaderName=="Cleveland"]+3, ypos)
                      , ypos = ifelse(LeaderName %in% c('Nixon'),
                                      ypos+3, ypos)
                      , ypos = ifelse(LeaderName %in% c('Grant'),
                                      ypos+1, ypos)
                      , ypos = ifelse(LeaderName %in% c('Fillmore'),
                                      ypos+2, ypos)
                      , ypos = ifelse(LeaderName %in% c('Buchanan'),
                                      ypos-3, ypos)
                      , ypos = ifelse(LeaderName %in% c('G. W. Bush'),
                                      ypos+4, ypos)
                      , ypos = ifelse(LeaderName %in% c('Jackson'),
                                      ypos-1, ypos)
                      , ypos = ifelse(LeaderName %in% c('L. Johnson'),
                                      ypos+3, ypos)
                      , ypos = ifelse(LeaderName %in% c('Carter'),
                                      ypos-2, ypos)
                      
)

duration.df$label[duplicated(duration.df$label)] <- ""

xoff <- 0.43
mysw(home.dir, "5-Figures")

dfmeans <- data.frame(x=c(northx, southx), 
                      y=c(mean(duration.df$WtdMean[duration.df$Region == 'Non-South']),
                          mean(duration.df$WtdMean[duration.df$Region == 'South'])),
                      colvec=colvec)


pdf(file = FileName('PresDurationBilateral', '', 'pdf', replace = TRUE),
    width = 4, height = 8)
(ggplot(data = subset(duration.df),
        #, #Uob == 1 | LeaderName %in% c('Kennedy')),
        aes(color = Region))
 + facet_wrap(~variable)
 + geom_point(data=dfmeans, aes(x,y),  colour=colvec,  shape=18, size=6)
 + annotate('segment', linetype = 'solid', size = 1.3,
            xend = northx+2, x = northx - 2,
            y = mean(duration.df$WtdMean[duration.df$Region == 'Non-South']),
            yend = mean(duration.df$WtdMean[duration.df$Region == 'Non-South']),
            color = north.color)
 + annotate('text', label = 'mean', x = northx - 2.2, hjust = 1,
            color = north.color,
            y = mean(duration.df$WtdMean[duration.df$Region == 'Non-South']))
 + annotate('segment', linetype = 'solid', size = 1.3, xend = southx-2,
            color = south.color,
            x = southx + 2,
            y = mean(duration.df$WtdMean[duration.df$Region == 'South']),
            yend = mean(duration.df$WtdMean[duration.df$Region == 'South']))
 + annotate('text', label = 'mean', x = southx + 2.2, hjust = 0,
            color = south.color,
            y = mean(duration.df$WtdMean[duration.df$Region == 'South']))
 + geom_point(aes(x = xpos, y = value, ## alpha = 1/Nsame, 
                  size = MIDs),
              shape = 1)
 + geom_text(aes(x = xpos + ifelse(hjust == 1, -xoff, xoff),
                 y = ypos, label = label, ## alpha = 1/Nsame,
                 hjust = hjust), size = 3)
 + theme_bw()
 + labs(y = "Average Duration of Disputes (in Days)", x = NULL)
 + scale_alpha_identity()
 + scale_color_manual(values = c(north.color, south.color))
 + scale_y_continuous(breaks = c(0, 50, 100, 150, 200, 250,  maxvalue))
 + scale_x_continuous(breaks = c(northx, southx)
                      , labels = c('Non-Southern                ', '         Southern')   ##NULL
                      , limits = c(-8, 8)
 )
 + theme(axis.text.x = element_text(colour=colvec), axis.text=element_text(size=14))
 + theme(panel.grid.minor = element_blank(), axis.ticks = element_blank(),
         strip.text = element_text(size = 20))
 + guides(size = FALSE, color = FALSE)
)
dev.off()



#### Outcome ####

outcome.df <- droplevels(subset(pres.bilateral.melt, variable == 'Outcome'))

minvalue <- round(min(outcome.df$value),1)
maxvalue <- round(max(outcome.df$value),1)
diffvalue <- maxvalue-minvalue
yadj <- 0.06

outcome.df <- mutate(outcome.df
                     , label = ifelse(LeaderName %in% c('Arthur', 'Buchanan'),
                                      'Arthur & Buchanan', label)
                     , ypos = ifelse(label=='Arthur & Buchanan', ypos-.02 + yadj, ypos)
                     , label = ifelse(LeaderName %in% c('Cleveland', 'Ford'),
                                      'Cleveland & Ford', label)
                     , ypos = ifelse(label=='Cleveland & Ford', ypos-.04 + yadj, ypos)
                     , label = ifelse(LeaderName %in% c('Eisenhower', 'Hayes'),
                                      'Eisenhower & Hayes', label)
                     , ypos = ifelse(label=='Eisenhower & Hayes', ypos-.06+ yadj, ypos)
                     , label = ifelse(LeaderName %in% c('Fillmore', 'Harding'),
                                      'Fillmore & Harding', label)
                     , ypos = ifelse(label=='Fillmore & Harding', ypos-.08+ yadj, ypos)
                     , label = ifelse(LeaderName %in% c('Taft', 'G. H. W. Bush'),
                                      'Taft & G. H. W. Bush', label)
                     , ypos = ifelse(label=='Taft & G. H. W. Bush', ypos-.1+ yadj, ypos)
                     , label = ifelse(LeaderName %in% c('Harrison', 'Lincoln'),
                                      'Harrison & Lincoln', label)
                     , ypos = ifelse(label=='Harrison & Lincoln', ypos-.12+ yadj, ypos)
                     , label = ifelse(LeaderName %in% c('Obama', 'Nixon'),
                                      'Obama & Nixon', label)
                     , ypos = ifelse(label=='Obama & Nixon', ypos-.14+ yadj, ypos)
                     , label = ifelse(LeaderName %in% c('Van Buren', 'Truman'),
                                      'Van Buren & Truman', label)
                     , ypos = ifelse(label=='Van Buren & Truman', ypos-.16+ yadj, ypos)
                     , ypos = ifelse(label=='Reagan', ypos + 0.02, ypos)
)

outcome.df$label[duplicated(outcome.df$label)] <- ""
xoff <- 0.43
mysw(home.dir, "5-Figures")

dfmeans <- data.frame(x=c(northx, southx), 
                      y=c(mean(outcome.df$WtdMean[outcome.df$Region == 'Non-South']),
                          mean(outcome.df$WtdMean[outcome.df$Region == 'South'])),
                      colvec=colvec)


pdf(file = FileName('PresOutcomeBilateral', '', 'pdf', replace = TRUE),
    width = 4, height = 8)
(ggplot(data = subset(outcome.df),
        #, #Uob == 1 | LeaderName %in% c('Kennedy')),
        aes(color = Region))
 + facet_wrap(~variable)
 + geom_point(data=dfmeans, aes(x,y),  colour=colvec,  shape=18, size=6)
 + annotate('segment', linetype = 'solid', size = 1.3,
            xend = northx+2, x = northx - 2,
            y = mean(outcome.df$WtdMean[outcome.df$Region == 'Non-South']),
            yend = mean(outcome.df$WtdMean[outcome.df$Region == 'Non-South']),
            color = north.color)
 + annotate('text', label = 'mean', x = northx - 2.2, hjust = 1,
            color = north.color,
            y = mean(outcome.df$WtdMean[outcome.df$Region == 'Non-South']))
 + annotate('segment', linetype = 'solid', size = 1.3, xend = southx-2,
            color = south.color,
            x = southx + 2,
            y = mean(outcome.df$WtdMean[outcome.df$Region == 'South']),
            yend = mean(outcome.df$WtdMean[outcome.df$Region == 'South']))
 + annotate('text', label = 'mean', x = southx + 2.2, hjust = 0,
            color = south.color,
            y = mean(outcome.df$WtdMean[outcome.df$Region == 'South']))
 + geom_point(aes(x = xpos, y = value, ## alpha = 1/Nsame, 
                  size = MIDs),
              shape = 1)
 + geom_text(aes(x = xpos + ifelse(hjust == 1, -xoff, xoff),
                 y = ypos, label = label, ## alpha = 1/Nsame,
                 hjust = hjust), size = 3)
 + theme_bw()
 + labs(y = "Proportion of Disputes Won - Proportion Lost", x = NULL)
 + scale_alpha_identity()
 + scale_color_manual(values = c(north.color, south.color))
 + scale_y_continuous(breaks = c(-0.2, 0,0.2, 0.4, 0.6, 0.8, maxvalue))
 + scale_x_continuous(breaks = c(northx, southx)
                      , labels = c('Non-Southern                ', '         Southern')   ##NULL
                      , limits = c(-8, 8)
 )
 + theme(axis.text.x = element_text(colour=colvec), axis.text=element_text(size=14))
 + theme(panel.grid.minor = element_blank(), axis.ticks = element_blank(),
         strip.text = element_text(size = 20))
 + guides(size = FALSE, color = FALSE)
)
dev.off()





